package ljl.codetop300;

public class _189_rotate_arr {

  /**
   * 我这个居然是最笨逼的，没人这么做
   */
  static class tes {
    // 1 2 3 4 1 2 3 4
    public void rotate(int[] nums, int k) {
      int len = nums.length;
      int[] tmp = new int[len * 2];
      System.arraycopy(nums, 0, tmp, 0, len);
      System.arraycopy(nums, 0, tmp, len, len);
      k %= len;
      if (k == 0) return;
      System.arraycopy(tmp, len - k, nums, 0, len);
    }
  }

  /**
   * 翻转
   */
  static class swap {
    public void rotate(int[] nums, int k) {
      k %= nums.length;
      reverse(nums, 0, nums.length - 1);
      reverse(nums, 0, k - 1);
      reverse(nums, k, nums.length - 1);
    }
    void reverse(int[] nums, int i, int j) {
      while (i < j) {
        int t = nums[i]; nums[i] = nums[j]; nums[j] = t;
        i++;
        j--;
      }
    }
  }

  static class math {
    public void rotate(int[] nums, int k) {
      int len = nums.length;
      k %= len;
      int round = gcd(len, k);
      for (int i = 0; i < round; i++) {
        int currentIndex = i;
        int currentValue = nums[currentIndex];
        do {
          int nextIndex = (currentIndex + k) % len;
          int nextValue = nums[nextIndex];
          nums[nextIndex] = currentValue;
          currentIndex = nextIndex;
          currentValue = nextValue;
        } while (currentIndex != i);
      }
    }
    int gcd(int a, int b) {
      return b == 0 ? a : gcd(b, a % b);
    }
  }

}
